﻿using System;
using System.Windows;
using System.Windows.Controls;
using PSE.Deployment.ConfigurationManager.Controls;
using PSE.Deployment.SetupBase;
using PSE.Deployment.SetupBase.Configuration;
using PSE.Deployment.SetupBase.Parameter.Enums;
using PSE.Deployment.SetupBase.UI;
using Sinacor.Deployment.Setup.Main;

namespace Sinacor.Deployment.Setup.UI
{
    /// <summary>
    /// Interaction logic for MSMQ.xaml
    /// </summary>
    public partial class MSMQ : BaseWizardWindow
    {
        #region Propriedades
        public Queue MSMQConfiguration { get; set; }
        #endregion

        #region Construtor
        public MSMQ(Queue queue)
        {
            this.InitializeComponent();

            MSMQConfiguration = queue;
            this.DataContext = MSMQConfiguration;

            CheckAllowNext();

            if (SetupMainFactory.GetInstance<SinacorMain>().Parameter.Prerequisites.MSMQ == PrerequisitesStatus.NotOk)
            {
                rdbRemoto.IsChecked = true;
                rdbLocal.IsEnabled = false;
                MSMQConfiguration.MachineName = string.Empty;
            }

            TxtMachineName.IsEnabled = rdbRemoto.IsChecked.GetValueOrDefault();
        }
        #endregion

        #region Método de Habilitação do Botão Próximo
        public void CheckAllowNext()
        {
            this.AllowNext = (!String.IsNullOrEmpty(TxtMachineName.Text) && !String.IsNullOrEmpty(TxtQueueName.Text));
        }
        #endregion

        #region Evento do Botão Próximo
        protected override void OnNextClick(RoutedEventArgs e)
        {
            base.OnNextClick(e);

            if (rdbPrivate.IsChecked.GetValueOrDefault())
            {
                MSMQConfiguration.LocationType = PSE.Deployment.SetupBase.Configuration.Enums.QueueLocationType.Private;
            }
            else if (rdbPublic.IsChecked.GetValueOrDefault())
            {
                MSMQConfiguration.LocationType = PSE.Deployment.SetupBase.Configuration.Enums.QueueLocationType.Public;
            }

            if (!MSMQConfiguration.NoQueuePermitedInstallation && (MSMQConfiguration.InstallationType == PSE.Deployment.SetupBase.Configuration.Enums.QueueInstallationType.Remote) && !PSE.Deployment.Util.MSMQ.QueueExists(MSMQConfiguration.MachineName, MSMQConfiguration.FullPath))
            {
                if (ConfigurationMessageBox.Show("Não foi possível localizar a fila MSMQ (" + MSMQConfiguration.FullPath + ")." + Environment.NewLine + " Deseja prosseguir sem a fila estar criada?", "Atenção", ConfigurationMessageBoxButton.YesNo, ConfigurationMessageBoxImage.Question) == ConfigurationMessageBoxResult.No)
                {
                    MSMQConfiguration.NoQueuePermitedInstallation = false;
                    e.Handled = true;
                }
                else
                    MSMQConfiguration.NoQueuePermitedInstallation = true;
            }
        }
        #endregion

        #region Eventos de TextChanged dos TextBoxes para forçar teste de conexão
        private void TxtMachineName_TextChanged(object sender, TextChangedEventArgs e)
        {
            MSMQConfiguration.NoQueuePermitedInstallation = false;
            CheckAllowNext();
        }

        private void TxtQueueName_TextChanged(object sender, TextChangedEventArgs e)
        {
            MSMQConfiguration.NoQueuePermitedInstallation = false;
            CheckAllowNext();
        }
        #endregion

        private void rdbRemoto_Click(object sender, RoutedEventArgs e)
        {
            if (rdbLocal.IsChecked.GetValueOrDefault())
                TxtMachineName.Text = ".";
            else
                TxtMachineName.Text = string.Empty;

            TxtMachineName.IsEnabled = rdbRemoto.IsChecked.GetValueOrDefault();
        }
    }
}
